package com.hhb.test;

import com.hhb.mapper.UserMapper;
import com.hhb.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * 学习一级缓存跟二级缓存
 */
public class Demo07 {

    String resource = "mybatis-config.xml";
    InputStream inputStream = null;
    SqlSession openSession = null;
    SqlSession openSession1 = null;


    @Test
    public void test01()  {


        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            openSession = sqlSessionFactory.openSession();
            openSession1 = sqlSessionFactory.openSession();

            UserMapper mapper = openSession.getMapper(UserMapper.class);
            UserMapper mapper1 = openSession1.getMapper(UserMapper.class);
            User user1 = mapper.findUser(33);
            System.out.println(user1);
            openSession.close();

//            openSession.clearCache();
            User user2 = mapper1.findUser(33);
            System.out.println(user2);

            System.out.println(user1 == user2);

//            User user3 = mapper.findUserById(33);
//            System.out.println(user3);
//
//            User user4 = mapper.findUserById(33);
//            System.out.println(user4);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //openSession.close();
            openSession1.close();

        }

    }
}
